<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 2017/2/21
 * Time: 下午3:12
 */
class Business extends MY_Admin_Controller
{
    public function index()
    {
        $limit = 20;
        $page  = intval($this->input->get_post('page'));
        !$page && $page = 1;
        $offset = ($page - 1) * $limit;


        $cid         = (int)$this->input->get_post('cid');
        $opera_type  = (int)$this->input->get_post('opera_type');
        $by_business = (int)$this->input->get_post('by_business');
        $uid         = trim($this->input->get_post('uid'));
        $is_export   = (int)$this->input->get_post('is_export');
        $start_time  = trim($this->input->get_post('start_time'));
        $end_time    = trim($this->input->get_post('end_time'));


        $where = array();
        $cid && $where['cid'] = $cid;
        $opera_type && $where['type'] = $opera_type;
        $by_business && $where['by_business'] = $by_business;
        !empty($uid) && $where['uid'] = $uid;
        (!empty($start_time)) && $where['create_time >'] = date('Y-m-d H:i:s', strtotime($start_time));
        (!empty($start_time)) && $where['create_time <'] = date('Y-m-d H:i:s', strtotime($end_time));


        $this->load->model('finance/model_finance_base_log','fbl');
        $total = $this->fbl->count($where);
        if ($is_export)
        {
            $data  = $this->fbl->lists($offset, 100000, $where);
        }
        else
        {
            $data  = $this->fbl->lists($offset, $limit, $where);
        }


        $amount_total      = 0;
        $real_amount_total = 0;
        if ($cid)
        {
            $total_amount = $this->fbl->getOne($where, 'sum(amount) as amount_total, sum(real_amount) as real_amount_total');
            isset($total_amount['amount_total']) && $amount_total = $total_amount['amount_total'];
            isset($total_amount['real_amount_total']) && $real_amount_total = $total_amount['real_amount_total'];
        }


        //关联流水
        $bids = array_column($data, 'bid');


        if (!empty($bids))
        {
            $this->load->model('finance/Model_finance_stream','fs');
            $stream_data = $this->fs->getByIn('bid', $bids, array(), 'bid');


            foreach ($data as $k=>$v)
            {
                $bid = $v['bid'];
                $data[$k]['pre_amount']  = isset($stream_data[$bid]['pre_amount']) ? $stream_data[$bid]['pre_amount'] : 0;
                $data[$k]['back_amount'] = isset($stream_data[$bid]['back_amount']) ? $stream_data[$bid]['back_amount'] : 0;
            }
        }


        $config['total_rows'] = $total;
        $config['per_page'] = 20;
        $config['base_url'] = '/finance/Business/index';


        //分页
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $page_html = $this->pagination->create_links();


        //财务科目
        $this->load->model('finance/model_finance_category_conf', 'fcc');
        $category = $this->fcc->lists(OFFSET, 10000);


        if ($is_export)
        {
            $currency = config_item('currency');
            $fot = config_item('finance_operation_type');
            $fb = config_item('finance_business');


            $export_data[] = array
            (
                '业务总账ID','时光号','用户名称','业务线ID','业务线标题','所属业务线','操作类型','描述','金额(合计:'.f_price($amount_total).')',
                '实付金额(合计:'.f_price($real_amount_total).')','折扣','IP地址','币种','财务科目','发生时间','操作前金额','操作后金额'
            );


            foreach ($data as $v)
            {
                $v['discount'] = (int)$v['discount'];
                unset($v['aid'], $v['extend']);
                $v['by_business'] = (isset($fb[$v['by_business']]) ? $fb[$v['by_business']] : '未知');
                $v['type']        = (isset($fot[$v['type']]) ? $fot[$v['type']] : '未知');
                $v['amount']      = f_price($v['amount']);
                $v['pre_amount']  = f_price($v['pre_amount']);
                $v['back_amount'] = f_price($v['back_amount']);
                $v['real_amount'] = f_price($v['real_amount']);
                $v['discount']    = (($v['discount'] / 10).' 折');
                $v['currency']    = (isset($currency[$v['currency']]['name']) ? $currency[$v['currency']]['name'] : '未知');
                $v['cid']         = (isset($category[$v['cid']]['name']) ? $category[$v['cid']]['name'] : '');


                $export_data[] = $v;
            }
            //p($export_data);

            $this->load->service('common/service_common_helper');
            $this->service_common_helper->create_xls($export_data, $filename='business_record_'.date('Y-m-d').'.xls');
            //exportCsv('business_record.csv', $string);
            exit;
        }



        $data = array
        (
            'data'        => $data,
            'category'    => $category,
            'page_html'   => $page_html,
            'total_rows'  => $config['total_rows'],
            'cid'         => $cid,
            'opera_type'  => $opera_type,
            'by_business' => $by_business,
            'uid'         => $uid,
            'start_time'  => $start_time,
            'end_time'    => $end_time,
            'amount_total'      => $amount_total,
            'real_amount_total' => $real_amount_total,
        );


        $this->load->view('admin/finance/business/index', $data);
    }


    //private function
}